package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"add_client/proto"

	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
)

func main() {
	// 通过RPC调用其他程序中的Do方法

	// 1. 建立连接
	// 不使用加密认证 grpc.WithTransportCredentials(insecure.NewCredentials())
	conn, err := grpc.Dial(":8081", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("net.Dial failed, err:%v\n", err)
	}
	defer conn.Close()
	// 2. 发起调用
	client := proto.NewCalClient(conn) //连接
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)//1秒超时时间
	defer cancel()

	// res, err := client.Do(ctx, &proto.Req{X: 10, Y: 20, Op: proto.Op_ADD})
	res, err := client.Do(ctx, &proto.Req{X: 10, Y: 20, Op: proto.Op_SUB})
	// res, err := client.Do(ctx, &proto.Req{X: 10, Y: 20})
	if err != nil {
		log.Printf("client.Do failed, err:%v\n", err)
		return
	}

	// 打印结果
	fmt.Println(res.Sum)
	// fmt.Println(res.GetSum())
}
